﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.DataTask;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace CPPEI.Coolzon.UI
{
    /// <summary>
    /// 数据组件XProperty的定义文件
    /// </summary>
    public abstract partial class DataPart
    {
        #region fields
        UI.DataSource _xpDataSource;
        #endregion

        #region xProperties

        [Category("Data")]
        [DisplayName("数据源")]
        [Description("DataSource")]
        [Editor(typeof(XProperty.Editors.XDataSourceEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "DataSource", "数据源")]
        public UI.DataSource XDataSource
        {
            get
            {
                return _xpDataSource;
            }
            set
            {
                if(_xpDataSource != value)
                {
                    // 清除掉存放的数据信息
                    this.DataResult = null;
                    _xpDataSource = value;
                    Logger.DebugToTag(Library.LogTAG, string.Format("{0}数据源变更，清空数据，调用Refresh()", this.ToString()));
                    this.Refresh();
                }
            }
        }
        private ColorPlate _XColorPlate;
        [Category("Appearance")]
        [DisplayName("调色板")]
        [Description("ColorPlate")]
        [Editor(typeof(XProperty.Editors.XColorPlateEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Appearance, "ColorPlate", "调色板")]
        public ColorPlate XColorPlate
        {
            get
            {
                return _XColorPlate;
            }
            set
            {
                if(value != null)
                {
                    _XColorPlate = value;
                    Refresh();
                }
            }
        }

        /// <summary>
        /// 数据加载动画持续时间(秒), 默认1s
        /// </summary>
        [Category("Data")]
        [DisplayName("数据加载动画持续时间")]
        [Description("DataAnimationDuration")]
        [Editor(typeof(XProperty.Editors.XDoubleEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "DataAnimationDuration", "数据加载动画持续时间")]
        public double XDataAnimationDuration 
        {
            get
            {
                return (double)GetValue(DataAnimationDurationProperty);
            }
            set
            {
                SetValue(DataAnimationDurationProperty, value);
            }
        }

        /// <summary>
        /// 自动刷新数据
        /// </summary>
        [Category("Data")]
        [DisplayName("是否自动刷新数据")]
        [Description("IsAutoRefresh")]
        [Editor(typeof(XProperty.Editors.XBooleanEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "IsAutoRefresh", "是否自动刷新数据")]
        public bool XIsAutoRefresh 
        {
            get
            {
                return (bool)GetValue(IsAutoRefreshProperty);
            }
            set
            {
                SetValue(IsAutoRefreshProperty, value);
            }
        }

        /// <summary>
        /// 自动刷新数据的间隔时间(秒)
        /// </summary>
        [Category("Data")]
        [DisplayName("自动刷新数据的间隔")]
        [Description("AutoRefreshInterval")]
        [Editor(typeof(XProperty.Editors.XDoubleEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "AutoRefreshInterval", "自动刷新数据的间隔")]
        public double XAutoRefreshInterval 
        {
            get
            {
                return (double)GetValue(AutoRefreshIntervalProperty);
            }
            set
            {
                SetValue(AutoRefreshIntervalProperty, value);
            }
        }
        private DataFilter _XDataSeriesFilter;
        /// <summary>
        /// 数据串的排序过滤条件:SeriesId1,SeriesId2,
        /// </summary>
        [Category("Data")]
        [DisplayName("数据串过滤")]
        [Description("DataSeriesFilter")]
        [Editor(typeof(XProperty.Editors.XDataSeriesFilterEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "DataSeriesFilter", "数据串过滤", false)]
        public DataSeriesFilter XDataSeriesFilter
        {
            get
            {
                return (DataSeriesFilter)_XDataSeriesFilter;
            }
            set
            {
                _XDataSeriesFilter = value;
                if (XDataSource != null)
                {
                    this.DataResult = null;
                }
                Refresh();
            }
        }
        private DataFilter _XDataItemFilter;
        /// <summary>
        /// 数据项的排序过滤条件:ItemId1,ItemId2,
        /// </summary>
        [Category("Data")]
        [DisplayName("数据项过滤")]
        [Description("DataItemFilter")]
        [Editor(typeof(XProperty.Editors.XDataItemFilterEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Data, "DataItemFilter", "数据项过滤", false)]
        public DataItemFilter XDataItemFilter
        {
            get { return (DataItemFilter)_XDataItemFilter; }
            set
            {
                _XDataItemFilter = value;
                if (XDataSource != null)
                {
                    this.DataResult = null;
                }
                Refresh();
            }
        }

        #endregion

        #region Dependency properties

        
        /// <summary>
        /// Dependency property to Get/Set the DataAnimationDuration 
        /// </summary>
        public static DependencyProperty DataAnimationDurationProperty =
            DependencyProperty.Register("XDataAnimationDuration", typeof(double), typeof(DataPart), new PropertyMetadata(new PropertyChangedCallback(DataPart.OnDataAnimationDurationChanged)));

        private static void OnDataAnimationDurationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataPart dataPart = d as DataPart;
            dataPart.RaisePropertyChanged(e.Property.Name);
            dataPart.Refresh();
        }

           /// <summary>
        /// Dependency property to Get/Set the IsAutoRefresh 
        /// </summary>
        public static DependencyProperty IsAutoRefreshProperty =
            DependencyProperty.Register("XIsAutoRefresh", typeof(bool), typeof(DataPart), new PropertyMetadata(new PropertyChangedCallback(DataPart.OnIsAutoRefreshChanged)));

        private static void OnIsAutoRefreshChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataPart dataPart = d as DataPart;
            if (dataPart._timer == null)
            {
                dataPart._timer = new DispatcherTimer();
                dataPart._timer.Tick += dataPart._timer_Tick;
                dataPart._timer.Interval = TimeSpan.FromSeconds(dataPart.XAutoRefreshInterval);
            }
            dataPart._timer.IsEnabled = Convert.ToBoolean(e.NewValue);
            dataPart.RaisePropertyChanged(e.Property.Name);
            dataPart.Refresh();
        }

            /// <summary>
        /// Dependency property to Get/Set the AutoRefreshInterval 
        /// </summary>
        public static DependencyProperty AutoRefreshIntervalProperty =
            DependencyProperty.Register("XAutoRefreshInterval", typeof(double), typeof(DataPart), new PropertyMetadata(new PropertyChangedCallback(DataPart.OnAutoRefreshIntervalChanged)));

        private static void OnAutoRefreshIntervalChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataPart dataPart = d as DataPart;
            if(dataPart._timer != null)
            {
                dataPart._timer.Interval = TimeSpan.FromSeconds(Convert.ToDouble(e.NewValue));
            }
            dataPart.RaisePropertyChanged(e.Property.Name);
            dataPart.Refresh();
        }

        #endregion
    }
}
